<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/7 0007
 * Time: 16:27
 */

namespace Admin\Controller;

use Think\Page;

class SecondController extends AdminController
{

    public function add()
    {
        $this->display();
    }

    public function index()
    {
        $p = I('p', '1');
        $page_size = 20;
        $second = M('Seconds');
        $count = $second->count();
        $list = $second->order('update_time desc')->page($p . ',' . $page_size)->select();
        $page = new Page($count, $page_size);
        $this->assign('page', $page->show());
        $this->assign('list', $list);
        $this->display();
    }

    public function edit()
    {
        $id = I('id', '');
        $info = M('Seconds')->where(array('id' => $id))->find();
        if ($info) {
            $this->assign('info', $info);
            $this->display();
        } else {
            $this->error('查询失败!');
        }
    }

    public function update()
    {
        $id = I('id', '');
        $data['title'] = I('title', '');
        $data['contact'] = I('contact', '');
        $data['brand'] = I('brand', '');
        $data['type'] = I('type', '');
        $data['years'] = I('years', '');
        $data['show'] = I('show', 0);
        $data['company'] = I('company', '');
        $data['phone'] = I('phone', '');
        $data['address'] = I('address', '');
        $data['update_time'] = date('Y-m-d H:i:s');
        $data['remark'] = I('remark', '');
        $second = M('Seconds');
        $count = $second->where('id=' . $id)->count();
        if ($count) {
            $res = $second->where('id=' . $id)->data($data)->save();
            if ($res) {
                $this->success('修改成功!',U('index'));
            } else {
                $this->error('修改失败!', U('index'));
            }
        } else {
            $data['create_time'] = date('Y-m-d H:i:s');
            $res = $second->data($data)->add();
            if ($res) {
                $this->success('添加成功!',U('index'));
            } else {
                $this->error('添加失败!',U('index'));
            }
        }
    }

    public function delete()
    {
        $id = I('id', 0);
        if ($id) {
            $res = M('Seconds')->where('id=' . $id)->delete();
            if ($res) {
                $this->success("删除成功!");
            } else {
                $this->error("删除失败！");
            }
        } else {
            $this->error("删除失败!");
        }
    }

    public function mark()
    {
        $second = M('Seconds');
        $id = I('id', 0);
        $show = I('show',0);
        if ($show==0||$show==1) {
            $data['show'] = $show;
            $data = $second->where("id=" . $id)->data($data)->save();
            if ($data) {
                $this->success("修改成功!");
            } else {
                $this->error("修改失败!");
            }
        } else {
            $this->error("修改失败!");
        }
    }

}